home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / LDelayedTask / UDeleteTaskQueue.cp < prev    next >
Encoding:
Text File  |  1995-08-28  |  3.4 KB  |  133 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    UDeleteTaskQueue.cp                © 1995, Éric Forget. All rights reserved.
  3. // ===========================================================================
  4. //    
  5. //    ************************************************************************
  6. //    *                                                                      *
  7. //    *    Before using this code you should read the "License Agreement"     *
  8. //    *    document and agree with it.                                        *
  9. //    *                                                                      *
  10. //    ************************************************************************
  11. //
  12. //    UDeleteTaskQueue is an utility class to delete a task when it is clean
  13. //    to do so. We need this class because certain type of task (e.g. LTimerTask)
  14. //    do not permit to move memory at interrupt time.
  15. //
  16. // ---------------------------------------------------------------------------
  17. //
  18. //    Instruction Notes:
  19. //    -----------------
  20. //
  21. //    Normally, you should not need to call it.
  22. //
  23. // ---------------------------------------------------------------------------
  24.  
  25. #ifdef PowerPlant_PCH
  26. #include PowerPlant_PCH
  27. #endif
  28.  
  29. #include "UDeleteTaskQueue.h"
  30. #include <LDynamicArray.h>
  31. #include "LTask.h"
  32.  
  33.  
  34. // ---------------------------------------------------------------------------
  35. //        • Static members
  36. // ---------------------------------------------------------------------------
  37.  
  38.  
  39. LDynamicArray*        UDeleteTaskQueue::sTaskQ = nil;
  40. UDeleteTaskQueue*    UDeleteTaskQueue::sDeleteTaskQueue = nil;
  41.  
  42.  
  43.  
  44. // ---------------------------------------------------------------------------
  45. //        • UDeleteTaskQueue
  46. // ---------------------------------------------------------------------------
  47. //    Default constructor
  48.  
  49. UDeleteTaskQueue::UDeleteTaskQueue()
  50. {
  51. }
  52.  
  53.  
  54. // ---------------------------------------------------------------------------
  55. //        • ~UDeleteTaskQueue
  56. // ---------------------------------------------------------------------------
  57. //    Destructor
  58.  
  59. UDeleteTaskQueue::~UDeleteTaskQueue()
  60. {
  61.     if(sTaskQ != nil) {
  62.     
  63.         LTask    *theTask;
  64.         
  65.         for(Int32 i = sTaskQ->GetCount(); i > 0; i--) {
  66.         
  67.             sTaskQ->FetchItemAt(i, &theTask);
  68.             Assert_(!theTask->IsExecuting());
  69.             
  70.             delete theTask;
  71.             sTaskQ->RemoveItemsAt(1, i);
  72.         }
  73.         
  74.         delete sTaskQ;
  75.     }
  76. }
  77.  
  78.  
  79. // ---------------------------------------------------------------------------
  80. //        • AddTask
  81. // ---------------------------------------------------------------------------
  82. //    Add Task in the to delete queue
  83.  
  84. void
  85. UDeleteTaskQueue::AddTask(
  86.     LTask    *inTask)
  87. {
  88.     if (sTaskQ == nil) {            // Create queue if it doesn't exist
  89.         
  90.         sTaskQ = new LList;
  91.     }
  92.     
  93.     if (sDeleteTaskQueue == nil) {    // Create object if it doesn't exist
  94.         
  95.         sDeleteTaskQueue = new UDeleteTaskQueue;
  96.     }
  97.     
  98.                                     // Add to end of the to delete queue
  99.     sTaskQ->InsertItemsAt(1, arrayIndex_Last, &inTask);
  100.     sDeleteTaskQueue->StartRepeating();
  101. }
  102.  
  103.  
  104. // ---------------------------------------------------------------------------
  105. //        • SpendTime
  106. // ---------------------------------------------------------------------------
  107.  
  108. void
  109. UDeleteTaskQueue::SpendTime(
  110.     const EventRecord &/*inMacEvent*/)
  111. {
  112.     LTask    *theTask;
  113.     
  114.     
  115.     for(Int32 i = sTaskQ->GetCount(); i > 0; i--) {
  116.     
  117.         sTaskQ->FetchItemAt(i, &theTask);
  118.         if(!theTask->IsExecuting()) {
  119.         
  120.             delete theTask;
  121.             sTaskQ->RemoveItemsAt(1, i);
  122.         }
  123.     }
  124.     
  125.     if(sTaskQ->GetCount() == 0) {
  126.     
  127.         delete sTaskQ;
  128.         sTaskQ = nil;
  129.         delete sDeleteTaskQueue;
  130.         sDeleteTaskQueue = nil;
  131.     }
  132. }
  133.